package com.example.gameapplication.plant;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.util.Log;

import com.example.gameapplication.BaseObject;
import com.example.gameapplication.bullet.Bullet;
import com.example.gameapplication.R;
import com.example.gameapplication.bullet.DoubleBullet;
import com.example.gameapplication.bullet.EnemyBullet;
import com.example.gameapplication.database.DataUtils;

import java.util.ArrayList;
import java.util.Random;

public class SmallPlane extends BaseObject {
    ArrayList<EnemyBullet> enemyBullits;
    private Bitmap smallPlane;
    private int heightPixels;
    private int widthPixels;
    int moveType;//飞行方向。1左，2右
    long moveTime;//飞行指定方向的次数
    private long BulletAliveTime;

    public ArrayList<EnemyBullet> getEnemyBullits() {
        return enemyBullits;
    }

    public void setEnemyBullits(ArrayList<EnemyBullet> enemyBullits) {
        this.enemyBullits = enemyBullits;
    }

    public SmallPlane(Resources resources, Paint paint) {
        super(resources, paint);
        enemyBullits = new ArrayList<>();
    }

    @Override
    public void initBitmap() {
        smallPlane = BitmapFactory.decodeResource(resources, R.drawable.small);
        object_width = smallPlane.getWidth();
        object_height = smallPlane.getHeight() / 3;
        widthPixels = DataUtils.getGameViewWidth();
        heightPixels = DataUtils.getGameViewHight();
    }

    @Override
    public void drawSelf(Canvas canvas) {
        if (smallPlane.isRecycled())
            initBitmap();
        canvas.save();
        drawPlant(canvas);
        canvas.restore();
//        beAttack();//调到了DataUtils.attack()

    }



    //画飞机
    public void drawPlant(Canvas canvas) {
        for (int i = 0; i < enemyBullits.size(); i++) {//先画子弹
            EnemyBullet enemyBullit = enemyBullits.get(i);
            if (!enemyBullit.isAlive() && isAlive()&&getCd()<=0) {
                if (System.currentTimeMillis() - BulletAliveTime > 5000) {//每次回复子弹的时间不可低于4秒
                    enemyBullit.setSeat(midx, midy);
                    enemyBullit.setAlive(random.nextBoolean());//每次重新随机决定是否攻击
                    enemyBullit.setLv(getLv());//重新确定子弹的等级角度
                    BulletAliveTime = System.currentTimeMillis();
                }
            }
            enemyBullit.drawSelf(canvas);
        }
        if (getCd()>0){
            reduceCD();
            return;
        }
        if (isAlive()) {
            canvas.clipRect(leftx, lefty, rightx, righty);
            canvas.drawBitmap(smallPlane, leftx, lefty, paint);
            logic();
            addBullet();
        } else {
            drawDestroyed(canvas);
        }
    }

    private void addBullet() {
        if (enemyBullits.size() < 1) {//子弹集合，我们一会给他定义1个
            EnemyBullet e = new EnemyBullet(resources, paint, getLv(), this, mainPlane, 40);
            enemyBullits.add(e);
        }
    }

    //画破坏的机体
    public void drawDestroyed(Canvas canvas) {
        int y = (int) (currentFrame * object_height);
        canvas.clipRect(leftx, lefty, rightx, righty);
        canvas.drawBitmap(smallPlane, leftx, lefty - y, paint);
        currentFrame = currentFrame < 2 ? ++currentFrame : 0;

        if (deathTime > 0) {
                deathTime--;
        } else {
            deathTime = 20;
            setLv(getLv() < DataUtils.getSmallPaineMaxLv() ? getLv() + 1 : getLv());
            setAlive(true);
            setSeat(widthPixels / 2, 0);//20次动画之后，重新登场
        }
    }



    //飞行逻辑
    public void logic() {

        if (mainPlane.midy - midy < heightPixels / 4&&mainPlane.midy>midy)//y轴距离少于屏幕的四分一就向着你的飞机撞过去
            midx = midx > mainPlane.getMidx() ? midx - speed <= 0 ? 0 : midx - speed : midx + speed > widthPixels ? widthPixels : midx + speed;
        else {
//            midx = random.nextInt() % 2 == 0 ? midx - speed : midx + speed;
            if (moveTime <= 0 || moveType == 0)
                if (random.nextBoolean()) {
                    moveType = 1;
                    moveTime = random.nextInt(14);
                } else {
                    moveType = 2;
                    moveTime = random.nextInt(14);
                }
            switch (moveType) {
                case 1:
                    midx = midx - speed <= 0 ? 0 : midx - speed;
                    break;
                case 2:
                    midx = midx + speed >= widthPixels ? widthPixels : midx + speed;
                    break;
            }
            moveTime--;
        }
        midy = midy + speed;
        setSeat(this.midx, this.midy);
        if (midy > heightPixels) {
            midy = 0;
            resetLife();
            reSetCD();
        }
    }

    @Override
    public void dataDestroyed() {
        if (!smallPlane.isRecycled()) {
            smallPlane.recycle();
            for (EnemyBullet enemyBullet: enemyBullits) {
                enemyBullet.dataDestroyed();
            }
        }
    }

    //被攻击,子弹调用该方法。要是被打中，子弹不处理，就会出现多次击中，因为刷新页面好几次子弹才穿过敌机。
    public void beAttack() {
        //获取子弹
        ArrayList<Bullet> bullets = mainPlane.getBullets();
        for (Bullet bullet : bullets) {
            if (bullet instanceof DoubleBullet) {
                doubleBulletAttack((DoubleBullet) bullet);
            } else {
                bulletAttack(bullet);
            }
        }
        if (mainPlane.isAlive() && mainPlane.unbeatable <= 0 && mainPlane.leftx < rightx && mainPlane.rightx > leftx && mainPlane.lefty < righty && mainPlane.righty > righty) {
            if (mainPlane.getLv() > 1) {//最低1级，伤害是等级的倍数，所以不可以为0
                mainPlane.setLv(mainPlane.getLv() - 1);
                mainPlane.unbeatable = 100;
            } else {
                mainPlane.setAlive(false);
            }
        }
    }

    //普通子弹的攻击
    public void bulletAttack(Bullet bullet) {
        if (bullet.isAlive() && bullet.leftx < rightx && bullet.rightx > leftx && bullet.lefty < righty && bullet.righty > righty) {
//                bullet.setAlive(false);//这一句如果添加子这里而不是下面，则敌机在爆炸的时候依然阻碍子弹穿越。
            if (life > 0) {//战机生命对于0
                life = life - bullet.getLv() * 3;//子弹的伤害当作子弹等级的倍计算；
                bullet.setAlive(false);//子弹攻击之后，生命结束
                if (life <= 0) {
                    setAlive(false); //如果生命值小于0,战机生命完结
                }
            }
        }
    }

    public void doubleBulletAttack(DoubleBullet doubleBullet) {
        if (doubleBullet.isAlive()) {
            //先判断左边的子弹
            if (doubleBullet.isLeftIsAlive() && doubleBullet.leftx < rightx && doubleBullet.rightx > leftx && doubleBullet.lefty < righty && doubleBullet.righty > righty) {
//                bullet.setLeftIsAlive(false);//这一句如果添加子这里而不是下面，则敌机在爆炸的时候依然阻碍子弹穿越。
                if (life > 0) {//战机生命对于0
                    life = life - doubleBullet.getLv() * 4;//子弹的伤害当作子弹等级的倍计算；
                    doubleBullet.setLeftIsAlive(false);//子弹攻击之后，生命结束
                    if (life <= 0) {
                        setAlive(false); //如果生命值小于0,战机生命完结
                    }
                }
            }
            //在判断左边子弹
            if (doubleBullet.isRightIsAlive() && doubleBullet.leftx2 < rightx && doubleBullet.rightx2 > leftx && doubleBullet.lefty2 < righty && doubleBullet.righty2 > righty) {
//                bullet.setRightIsAlive(false);//这一句如果添加子这里而不是下面，则敌机在爆炸的时候依然阻碍子弹穿越。
                if (life > 0) {//战机生命对于0
                    life = life - doubleBullet.getLv() * 4;//子弹的伤害当作子弹等级的倍计算；
                    doubleBullet.setRightIsAlive(false);//子弹攻击之后，生命结束
                    if (life <= 0) {
                        setAlive(false); //如果生命值小于0,战机生命完结
                    }
                }
            }
        }
    }
}
